﻿var app = new Vue({
    el: '#app',

    data: {
        connection: null,
        server: {},
        devices: [],
        states: [
            'Offline',
            'BootLoader',
            'Online',
            'Host',
            'Recovery',
            'NoPermissions',
            'Sideload',
            'Unauthorized',
            'Authorizing',
            'Unknown'
        ],
        executor_status: [
            'Ready',
            'Running',
            'Suspend',
            'Error',
            'Complete',
            'Stop',
            'Disposed'
        ]
    },

    created() {
        let self = this;

        self.connection = new signalR.HubConnectionBuilder()
            .withUrl("/adb")
            .configureLogging(signalR.LogLevel.Information)
            .build();

        async function start() {
            try {
                await self.connection.start();
                console.log('connected');
            } catch (e) {
                console.error(e);
                setTimeout(() => start(), 3000);
            }
        }

        self.connection.onclose(async () => {
            await start();
        });

        self.connection.on('status',
            resp => {
                if (resp.isSuccess) {
                    self.server = resp;
                } else {
                    alert(resp.msg);
                    console.log(resp.exception);
                }
            });

        self.connection.on('devices',
            resp => {
                if (resp.isSuccess) {
                    self.devices = resp.data;
                } else {
                    alert(resp.msg);
                    console.log(resp.exception);
                }
            });

        self.connection.on('device_connected',
            device => {
                self.devices = self.devices.filter((v, i, s) => v.serial !== device.serial);
                self.devices.push(device);
            });

        self.connection.on('device_disconnected',
            device => {
                self.devices = self.devices.filter((v, i, s) => v.serial !== device.serial);
            });

        self.connection.on('executor_status_changed',
            executor => {
                console.log(executor);

                self.devices.forEach((v, i, s) => {
                    if (v.serial === executor.serial) {
                        v.executor_status = executor.status;
                        // v.executor_exception = executor.exception;
                    }
                });
            });

        self.connection.start().then(() => {
            console.log('connected.');
            self.connection.invoke('GetStatus').catch(console.error);
            self.connection.invoke('GetDevices').catch(console.error);
        });
    },

    methods: {
        executeScript: function (device) {
            this.connection.invoke('ExecuteScript', device.serial).catch(console.error);
        },

        // for test
        completeSubscription: function(subscription) {
            this.connection.invoke('CompleteSubscription', subscription).catch(console.error);
        }
    }
});
























